﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Data.SqlClient;
using System.Configuration;
using System.Globalization;
using System.Threading;
using ERPStudio;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap;


namespace ESRP
{
    class dscModule : ModuleClass
    {
        public static Boolean eUnitateBugetara = false; /// daca e unitate bugetara
        public TreeNode ConsultaArhiva = null;                                                /// 
        
        public static ToolStripMenuItem mn_Situatii_Financiare = null;
        public dscModule(Int16 codUnit, String numeUnit,String theme)
            : base(codUnit, numeUnit,theme)
        {
            workConnectionString = "ESRPConnectionString";
            ModuleName = "balcon";
        }
        /// <summary>
        /// returneaza ID-ul intern pentru autorizare - 
        /// deriva din numele modului si este o cheie de control
        /// </summary>
        /// <returns></returns>
        override public String GetModuleID()
        {
            return "01";
        }

        public void AfterLoadMenu(ToolStripMenuItem mn, Form vForm, Guid appid, Guid userID)
        {

            FrmMainMdi frm = mdiForm as FrmMainMdi;
            if (NaviBarTreeView != null)
            {
                ConsultaArhiva = NaviBarTreeView.Nodes[0];
                foreach(TreeNode tn in ConsultaArhiva.Nodes)
                    if (tn.Text == "Consultare Arhiva")
                    {
                        ConsultaArhiva = tn;
                        break;
                    }
            }

            mn_Situatii_Financiare = FindMenuItem(mn, "Situatii Financiare");
            if (!eUnitateBugetara && mn_Situatii_Financiare != null)
                mn_Situatii_Financiare.Visible = false;
            InitStoredProcedure("Init_BalCon");
 
            SqlCommand cmd = new SqlCommand("", StudioForm.DeschideConexiune("ESRPConnectionString")); ;
            try
            {
                cmd.CommandText = "INSERT INTO [ParamListe] ([CodUnitate],[IDLista],[Cod],[NrCrt],[Denumire],[Valoare],[ValoareImplicita],[Tip],[Validare],[SelRezumat],[User],[Host]) " +
                    " (SELECT " + CodUnitate.ToString() + ",[IDLista],[Cod],[NrCrt],[Denumire],[Valoare],[ValoareImplicita],[Tip],[Validare],[SelRezumat],system_user,host_name() " +
                    " FROM [ParamListe] a WHERE [User] ='0' AND Host='0' AND [CodUnitate]=0 AND NOT EXISTS(SELECT * FROM [ParamListe] b WHERE b.[CodUnitate]=" + CodUnitate.ToString() + " AND b.[user]=system_user and b.[host]=host_name() and b.[Cod]=a.[Cod] and a.[IdLista]=b.[IdLista]))";
                cmd.ExecuteNonQuery();
                String tabelaS = "BalconSetup";
                String campS = "TipUnitate";
                String tU = (String)StudioForm.GetSetupValue(tabelaS, campS, CodUnitate, cmd.Connection);
                if (tU == "B ")
                {
                    eUnitateBugetara = true;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                //FrmError err = new FrmError(ex);
                //err.ShowDialog();

            }
            finally
            {
                StudioForm.InchideConexiune(cmd.Connection);
            }


        }


        /// <summary>
        /// incarca meniul acestui modul
        /// </summary>
        /// <param name="myMenu"></param>
        public void LoadMenu(ToolStripMenuItem mn, Form vForm, Guid appid, Guid userID)
        {
            base.LoadMenu(mn, (FrmMainMdi)vForm, appid, userID);
        }


        public void Unitati_functionale(object sender, EventArgs e)
        {
            FrmMainMdi frm = mdiForm as FrmMainMdi;
            Assembly asm = frm.GetModule("erpforms");
            if (asm == null)
                return;
            String numeClasa = "ERPForms.UnitatiFrm";
            Type mModule = asm.GetType(numeClasa);

            if (mModule != null)
            {
                FrmRezumat newFrm = asm.CreateInstance(numeClasa, true, BindingFlags.Public | BindingFlags.Instance | BindingFlags.CreateInstance, null,
                   new object[] { this, false }, Thread.CurrentThread.CurrentCulture, new object[] { }) as FrmRezumat;

                newFrm.MdiParent = frm;
                newFrm.Show();
            }
        }

        public void Despre_Modul(object sender, EventArgs e)
        {
            AboutBox frm = new AboutBox();
            frm.ShowDialog();
        }

        public void Consultare_Arhiva(object sender, EventArgs e)
        {
            if (AnArhiva == String.Empty)
            {
                FrmMainMdi frm = mdiForm as FrmMainMdi;
                Assembly asm = frm.GetModule("erpforms");
                if (asm == null)
                    return;
                String numeClasa = "ERPForms.ConsultareArhivaFrm";
                Type mModule = asm.GetType(numeClasa);

                if (mModule != null)
                {
                    FrmRezumat newFrm = asm.CreateInstance(numeClasa, true, BindingFlags.Public | BindingFlags.Instance | BindingFlags.CreateInstance, null,
                       new object[] { this, ConsultaArhiva }, Thread.CurrentThread.CurrentCulture, new object[] { }) as FrmRezumat;

                    newFrm.MdiParent = frm;
                    newFrm.Show();
                }

            }
            else
            {
                ToolStripMenuItem mn_Consultare_Arhiva = null;
                ToolStripMenuItem mn_Parametrizare_Modul = null;

                mn_Consultare_Arhiva = FindMenuItem(menuMain, "Consultare Arhiva");
                mn_Parametrizare_Modul = FindMenuItem(menuMain, "Setari");

                AnArhiva = String.Empty;
                mn_Consultare_Arhiva.Text = "Consultare Arhiva";
                StudioForm.SetArhiva(AnArhiva);
                mn_Parametrizare_Modul.Visible = true;
                if (ConsultaArhiva != null)
                    ConsultaArhiva.Text = "Consultare Arhiva";
            }
            
        }

        public void Evidenta_Conturi(object sender, EventArgs e)
        {
            if (AnArhiva != String.Empty)
            {
                ArhConturiRezumatFrm afrm = new ArhConturiRezumatFrm(this);
                afrm.MdiParent = mdiForm;
                afrm.Show();
                return;
            }
            ConturiRezumatFrm frm = new ConturiRezumatFrm(this);
            frm.MdiParent = mdiForm;
            frm.Show();
        }

        public void GenerareNoteContabile(object sender, EventArgs e)
        {
            GenNoteContFrm frm = new GenNoteContFrm(this);
            frm.MdiParent = mdiForm;
            frm.EnableSearch = false;
            frm.Show();
        }

        public void Parametrizare_Liste(object sender, EventArgs e)
        {
            ParametrizareListeFrm frm = new ParametrizareListeFrm(this);
            frm.MdiParent = mdiForm;
            frm.Show();
        }

        public void Parametrizare_Import(object sender, EventArgs e)
        {
            ImportFrm frm = new ImportFrm(this);
            frm.MdiParent = mdiForm;
            frm.Show();
        }

        public void ConturiCorespondente(object sender, EventArgs e)
        {
            ConturiCorespFrm frm = new ConturiCorespFrm(this);
            frm.MdiParent = mdiForm;
            frm.Show();
        }

        public void Rapoarte(object sender, EventArgs e)
        {
            ToolStripDropDownItem di = (ToolStripDropDownItem)sender;
            switch (Convert.ToInt16(di.Tag))
            {
                case 1: //balanta
                case 2: //fisa cont
                case 4: //registrul jurnal
                case 5: //registrul de casa
                case 6: //extras de cont
                case 11: //executie
                case 13: //executie cheltuile
                    OptiuniRapoarteFrm frm = null;              
                    frm = new OptiuniRapoarteFrm(Convert.ToInt16(di.Tag), this);
                    frm.Text += "-" + di.Text;
                    frm.MdiParent = mdiForm;
                    frm.Show();
                    return;

            }

            RapoarteFrm frm2 = new RapoarteFrm(Convert.ToInt16(di.Tag),this);
            frm2.Text += "-" + di.Text; 
            frm2.MdiParent = mdiForm;
            frm2.Show();
        }

        public void ContExecutie(object sender, EventArgs e)
        {
            OptiuniRapoarteFrm frm = null;
            frm = new OptiuniRapoarteFrm(13, this);
            frm.Text += "-" + "Contul de executie (institutie)";
            frm.MdiParent = mdiForm;
            frm.Show();
        }

        public void ExecutieCheltuieli(object sender, EventArgs e)
        {
            OptiuniRapoarteFrm frm = null;
            frm = new OptiuniRapoarteFrm(11, this);
            frm.Text += "-" + "Executie Cheltuieli";
            frm.MdiParent = mdiForm;
            frm.Show();
        }

        public void OperatiuniBugetare(object sender, EventArgs e)
        {
            RapoarteFrm frm = new RapoarteFrm(12,this);
            frm.Text += "-" + "Operatiuni bugetare";
            frm.MdiParent = mdiForm;
            frm.Show();
        }

        public void ContRezultat(object sender, EventArgs e)
        {
            RapoarteFrm frm = new RapoarteFrm(14,this);
            frm.Text += "-" + "Cont de rezultat patrimonial";
            frm.MdiParent = mdiForm;
            frm.Show();
        }

        public void ContProfit(object sender, EventArgs e)
        {
            RapoarteFrm frm = new RapoarteFrm(9,this);
            frm.Text += "-" + "Cont de profit si pierdere";
            frm.MdiParent = mdiForm;
            frm.Show();
        }

        public void VenituriSiCheltuieli(object sender, EventArgs e)
        {
            RapoarteFrm frm = new RapoarteFrm(15,this);
            frm.Text += "-" + "Sit. venituri si cheltuieli";
            frm.MdiParent = mdiForm;
            frm.Show();
        }

        
        public void Jurnale(object sender, EventArgs e)
        {
            ToolStripDropDownItem itm = sender as ToolStripDropDownItem;
            if (eUnitateBugetara)
                itm.Text = "Capitole (Clasificatie Bugetare)";
            FrmMainMdi frm = mdiForm as FrmMainMdi;
            Assembly asm = frm.GetModule("erpforms");
            if (asm == null)
                return;

            String numeClasa = "ERPForms.JurnaleFrm";
            Type mModule = asm.GetType(numeClasa);

            if (mModule != null)
            {
                FrmRezumat newFrm = asm.CreateInstance(numeClasa, true, BindingFlags.Public | BindingFlags.Instance | BindingFlags.CreateInstance, null,
                   new object[] { this, eUnitateBugetara }, Thread.CurrentThread.CurrentCulture, new object[] { }) as FrmRezumat;

                newFrm.MdiParent = frm;
                newFrm.Show();
            }

        }

        public void Evidenta_NotaContabila(object sender, EventArgs e)
        {
            if (AnArhiva != String.Empty)
            {
                ArhFormulaContabilaFrm afrm = new ArhFormulaContabilaFrm(this);
                afrm.MdiParent = mdiForm;
                afrm.Show();
                return;      
            }
            FormulaContabilaFrm frm = new FormulaContabilaFrm(this);
            frm.MdiParent = mdiForm;
            frm.Show();
        }

        public void Parametrizare(object sender, EventArgs e)
        {
            ///configurare modul
            #region Configurare Modul BalCon
            FrmMainMdi frm = mdiForm as FrmMainMdi;
            Assembly asm = frm.GetModule("erpforms");
            if (asm == null)
                return;
            String numeClasa = "ERPForms.BalconSetupFrm";
            Type mModule = asm.GetType(numeClasa);

            if (mModule != null)
            {
                FrmCard newFrm = asm.CreateInstance(numeClasa, true, BindingFlags.Public | BindingFlags.Instance | BindingFlags.CreateInstance, null,
                   new object[] { this }, Thread.CurrentThread.CurrentCulture, new object[] { }) as FrmCard;
                //newFrm.MdiParent = mdiForm;
                newFrm.ShowDialog();
                eArhivaEditabila = Convert.ToBoolean(mModule.InvokeMember("GetArhivaEditabila", BindingFlags.InvokeMethod, null, newFrm, new object[] { }));
            }
            #endregion
            ///configurare modul-----------------------------------
        }

        public void SeriiNumerice(object sender, EventArgs e)
        {
            FrmMainMdi frm = mdiForm as FrmMainMdi;
            Assembly asm = frm.GetModule("erpforms");
            if (asm == null)
                return;
            String numeClasa = "ERPForms.SeriiNumericeRezumatFrm";
            Type mModule = asm.GetType(numeClasa);

            if (mModule != null)
            {
                FrmRezumat newFrm = asm.CreateInstance(numeClasa, true, BindingFlags.Public | BindingFlags.Instance | BindingFlags.CreateInstance, null,
                     new object[] { this }, Thread.CurrentThread.CurrentCulture, new object[] { }) as FrmRezumat;
                newFrm.MdiParent = frm;
                newFrm.Show();
            }
        }

        public void Sabloane_Note_Contabile(object sender, EventArgs e)
        {
            SablonNoteContabileFrm frm = new SablonNoteContabileFrm(this);
            frm.MdiParent = mdiForm;
            frm.Show();
        }

        public void Semnaturi(object sender, EventArgs e)
        {
            FrmMainMdi frm = mdiForm as FrmMainMdi;
            Assembly asm = frm.GetModule("erpforms");
            if (asm == null)
                return;

            String numeClasa = "ERPForms.SemnaturiFrm";
            Type mModule = asm.GetType(numeClasa);

            if (mModule != null)
            {
                FrmRezumat newFrm = asm.CreateInstance(numeClasa, true, BindingFlags.Public | BindingFlags.Instance | BindingFlags.CreateInstance, null,
                   new object[] { this }, Thread.CurrentThread.CurrentCulture, new object[] { }) as FrmRezumat;

                newFrm.MdiParent = frm;
                newFrm.Show();
            }
        }

        public void Copiaza_analitice(object sender, EventArgs e)
        {
            CopiazaAnaliticeFrm frm = new CopiazaAnaliticeFrm(this);
            frm.MdiParent = mdiForm;
            frm.Show();
        }

		public void Surse_Finantare(object sender, EventArgs e)
		{
			SurseFinantareFrm frm = new SurseFinantareFrm(this);
			frm.MdiParent = mdiForm;
			frm.Show();
		}

        public void Sector_Bugetar(object sender, EventArgs e)
        {
			SectorBugetarFrm frm = new SectorBugetarFrm(this);
            frm.MdiParent = mdiForm;
            frm.Show();
        }


    }
}
